package com.example.dryyzx.controller;

import com.example.dryyzx.dao.NursingContentMapper;
import com.example.dryyzx.encapsulation.NursingContentPageEncapsulation;
import com.example.dryyzx.encapsulation.NursingContentPageEncapsulation2;
import com.example.dryyzx.encapsulation.NursingPageEncapsulation;
import com.example.dryyzx.encapsulation.NursingPageEncapsulation2;
import com.example.dryyzx.entity.NursingContent;
import com.example.dryyzx.entity.JsonResult;
import com.example.dryyzx.entity.NursingContent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Controller
public class NursingContentController {

    @Autowired
    private NursingContentMapper nursingContentMapper;
    public List<NursingContentPageEncapsulation> search_nursingContent(int limit,int offset,String name){
        List<NursingContent> nursingContent = nursingContentMapper.selectNameByLimitAndOffset(limit,offset,name);
        List<NursingContentPageEncapsulation> targetContent = new ArrayList<NursingContentPageEncapsulation>();
        if (nursingContent.size()!= 0) {
            for (int i = 0; i < nursingContent.size(); i++) {
                NursingContentPageEncapsulation tmp = new NursingContentPageEncapsulation();
                tmp.id = nursingContent.get(i).getId();
                tmp.name = nursingContent.get(i).getName();
                if (nursingContent.get(i).getValueAddedServices()== true) {
                    tmp.service = "是";
                } else tmp.service = "否";

                tmp.price=nursingContent.get(i).getPrice().toString();

                if (nursingContent.get(i).getEnable() == true) {
                    tmp.status = "启用";
                } else tmp.status = "禁用";
                tmp.remarks = nursingContent.get(i).getRemarks();
                targetContent.add(i, tmp);
            }
        }
        return targetContent;
    }
    @RequestMapping(value = "nursingcontent_page/getNursingContent", method = RequestMethod.POST)
    public @ResponseBody
    String getNursingContent(int page, HttpSession session) {
        int data_per_page = 6;
        int total_data_no = nursingContentMapper.countContent();
        if(total_data_no<=0)
            return new JsonResult(false, "护理内容表为空").toString();

        int offset=(page-1)*data_per_page;
        int limit;
        if(total_data_no-offset<data_per_page)
            limit=total_data_no-offset;
        else
            limit=data_per_page;
        if(total_data_no<=offset)
            return new JsonResult(false, "页数过大，数据不够").toString();

        List<NursingContent> content=nursingContentMapper.selectByLimitAndOffset(limit,offset);
        List<NursingContentPageEncapsulation> targetContent = new ArrayList<NursingContentPageEncapsulation>();
        for(int i=0;i<limit;i++){
            NursingContentPageEncapsulation tmp = new NursingContentPageEncapsulation();
            tmp.id = content.get(i).getId();
            tmp.name=content.get(i).getName();
            if (content.get(i).getValueAddedServices()== true) {
                tmp.service = "是";
            } else tmp.service = "否";

                tmp.price=content.get(i).getPrice().toString();

            if (content.get(i).getEnable() == true) {
                tmp.status = "启用";
            } else tmp.status = "禁用";
            tmp.remarks = content.get(i).getRemarks();
            targetContent.add(i, tmp);
        }
        NursingContentPageEncapsulation2 total_data=new NursingContentPageEncapsulation2();
        total_data.nursing_content_list=targetContent;
        total_data.total_page=(total_data_no-1)/data_per_page+1;
        return new JsonResult(true, total_data).toString();
    }
    @RequestMapping(value = "nursingcontent_page/search_by_name", method = RequestMethod.POST)
    public @ResponseBody
    String search_by_name(int page,String name, HttpSession session) {
        if(name != null && name.trim().length()!= 0) {
            int data_per_page =6;
            int total_data_no =nursingContentMapper.countByName(name);
            if(total_data_no<=0) return new JsonResult(false,"找不到目标护理内容！请重新输入").toString();

            int offset=(page-1)*data_per_page;
            int limit;
            if(total_data_no-offset<data_per_page)
                limit=total_data_no-offset;
            else
                limit=data_per_page;
            if(total_data_no<=offset)
                return new JsonResult(false, "页数过大，数据不够").toString();

            List<NursingContentPageEncapsulation> targetContent = new ArrayList<NursingContentPageEncapsulation>();
            targetContent = search_nursingContent(limit,offset,name);

            NursingContentPageEncapsulation2 total_data = new NursingContentPageEncapsulation2();
            total_data.nursing_content_list = targetContent;
            total_data.total_page = (total_data_no - 1) / data_per_page + 1;
            return new JsonResult(true, total_data).toString();

        }else return new JsonResult(false,"输入不允许为空！请输入要查找的护理内容的名字").toString();
    }
    @RequestMapping(value = "nursingcontent_page/editNursingContent", method = RequestMethod.POST)
    public @ResponseBody
    void editNursingContent(@RequestParam("id")String id,@RequestParam("name")String name,@RequestParam("service")String service,@RequestParam("price")String price,@RequestParam("status")String status,@RequestParam("remarks")String remarks, HttpSession session) {
        NursingContent nursingContent=nursingContentMapper.selectByPrimaryKey(Integer.valueOf(id));
        nursingContent.setName(name);
        if(service.equals("是"))
            nursingContent.setValueAddedServices(true);
        else nursingContent.setValueAddedServices(false);
        nursingContent.setPrice(Integer.valueOf(price));
        if(status.equals("启用"))
            nursingContent.setEnable(true);
        else nursingContent.setEnable(false);
        nursingContent.setRemarks(remarks);
        nursingContentMapper.updateByPrimaryKeySelective(nursingContent);
    }
    @RequestMapping(value = "nursingconfig_page/getNursingMenu", method = RequestMethod.POST)
    public @ResponseBody
    String  getNursingMenu() {
        List<String> allNames=new ArrayList<>();
        allNames=nursingContentMapper.getContentNames();
        if(allNames.size()!=0){
            return new JsonResult(true,allNames).toString();
        }
        else return new JsonResult(false,"当前没有可供配置的护理内容").toString();
    }
}
